/*
 * Created on 2007-2-18
 *
 * TODO 
 */
package jacky.lanlan.song.extension.struts.action;

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jacky.lanlan.song.extension.struts.mock.DelegatingValueStack;
import jacky.lanlan.song.extension.struts.resource.Downloadable;
import jacky.lanlan.song.io.FileUtils;
import jacky.lanlan.song.io.IOUtils;
import jacky.lanlan.song.resource.ResourceUtils;
import jacky.lanlan.song.web.Scope;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.validator.LazyValidatorForm;

import org.junit.*;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

/**
 * @author Jacky.Song
 *
 * 测试POJOAction。
 */
public class POJOTestActionTest_PlainJUnit {

	private DelegatingValueStack mock;
	private ServletContext context;
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private POJOTestAction action;
	
	@Before
	public void onSetUp() {
		action=new POJOTestAction();
		mock=new DelegatingValueStack();
		context=mock(ServletContext.class);
		req=mock(HttpServletRequest.class);
		resp=mock(HttpServletResponse.class);
		mock.setMockContext(context);
		mock.setMockRequest(req);
		mock.setMockResponse(resp);
	}

	@Test
	public void testInvokeBusinessMethod() throws Exception {
		mock.setAttribute("param", "value_reqParam",Scope.Request);
		
		LazyValidatorForm form=new LazyValidatorForm();
		form.set("aaa", "string value");
		form.set("bbb", "111");

		String distination=action.businessMethod(mock, form.getMap());
		//检验结果
		assertEquals("success",distination);
    assertEquals("out_request", mock.getAttribute("ccc",Scope.Request));
    assertEquals("out_session", mock.getAttribute("ddd",Scope.Session));
	}
	
	@Test
	public void testInvokeAnotherBusinessMethod() throws Exception {
		mock.setAttribute("param", "value_reqParam2",Scope.Request);
		
		LazyValidatorForm form=new LazyValidatorForm();
		form.set("aaa", "string value2");
		form.set("bbb", 222);
		
		//执行Action方法
		String distination=action.anotherBusinessMethod(mock, BeanUtils.describe(form));
		//检验结果
		assertEquals("failure",distination);
		assertEquals("out_request2", mock.getAttribute("ccc",Scope.Request));
		assertEquals("out_session2", mock.getAttribute("ddd",Scope.Session));
	}
	
	@Test
	public void testDownload() throws Exception {
		//得到log4j.properties的内容作为参考
		File log4j=ResourceUtils.getFile("classpath:log4j.properties");
		byte[] expectContent=FileUtils.readFileToByteArray(log4j);
		
		Downloadable d=action.download(null, null);
		assertEquals("application/properties", d.getContentType());
		byte [] actualContent=IOUtils.toByteArray(d.getInputStream());
		
		assertArrayEquals("应该得到log4j.properties文件的字节流",expectContent, actualContent);
	}
	
	@After
	public void onTearDown() {
		context=null;
		req=null;
		resp=null;
		action=null;
	}
	
}
